//
//  PersonalCenterViewController.m
//  LiteraryTune
//
//  Created by 神州四达 on 2018/1/15.
//  Copyright © 2018年 cxd. All rights reserved.
//

#import "PersonalCenterViewController.h"
#import "LoginViewController.h"
#import "MemberCenterTopTableViewCell.h"
#import "MemberCenterListTableViewCell.h"
#import "MemerBroswerHistoryViewController.h"
#import "MemberInfoEditViewController.h"
#import "MemberPwEditViewController.h"
#import "LoginViewController.h"

@interface PersonalCenterViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UIButton *rightBtn;
@property (nonatomic, strong)UITableView *myTableView;

@end

@implementation PersonalCenterViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    MemberCenterTopTableViewCell *cell = [self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
//    if ([NSString checkNullWithNameString:[CXDHelper getUserAccountName]]) {
//        cell.nameLaebl.text = @"暂未登录";
//
//        LoginViewController *loginVC = [[LoginViewController alloc] init];
//        loginVC.backHome = YES;
//        [self.navigationController pushViewController:loginVC animated:YES];
//        return;
//    }else {
        if ([CXDHelper getUserLoginStatus]) {
            cell.nameLaebl.text = [NSString stringWithFormat:@"会员：%@",[CXDHelper getUserAccountName]];
            self.navigationItem.rightBarButtonItem = nil;
        }else {
            cell.nameLaebl.text = @"暂未登录";
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightBtn];
        }
    [self.myTableView reloadData];
//    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightBtn];
    [self buildUI];
}

- (void)buildUI {
    [self.view addSubview:self.myTableView];
}

#pragma mark - event response
- (void)rightBtnClickAction {
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    [self.navigationController pushViewController:loginVC animated:YES];
}

#pragma mark - 懒加载
- (UIButton *)rightBtn {
    if (!_rightBtn) {
        _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightBtn.frame = CGRectMake(0, 0,40 , 40);
        [_rightBtn setTitle:@"登录" forState:UIControlStateNormal];
        _rightBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_rightBtn addTarget:self action:@selector(rightBtnClickAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightBtn;
}

- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) style:UITableViewStylePlain];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_myTableView registerNib:[UINib nibWithNibName:NSStringFromClass([MemberCenterTopTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([MemberCenterTopTableViewCell class])];
        [_myTableView registerNib:[UINib nibWithNibName:NSStringFromClass([MemberCenterListTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([MemberCenterListTableViewCell class])];
    }
    return _myTableView;
}

#pragma mark - 协议实现
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    return 5;
    return [CXDHelper getUserLoginStatus]? 5: 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        MemberCenterTopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MemberCenterTopTableViewCell class])];
        if ([NSString checkNullWithNameString:[CXDHelper getUserAccountName]]) {
            cell.nameLaebl.text = @"暂未登录";
        }else {
            if ([CXDHelper getUserLoginStatus]) {
                cell.nameLaebl.text = [NSString stringWithFormat:@"会员：%@",[CXDHelper getUserAccountName]];
            }else {
                cell.nameLaebl.text = @"暂未登录";
            }
        }
        return cell;
    }
    MemberCenterListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MemberCenterListTableViewCell class])];
    NSArray *nameArr = @[@"浏览记录",@"信息修改",@"密码修改",@"退出登录"];
    NSArray *imageArr = @[@"browers",@"infoEdit",@"pwEdit",@"exitLogin"];
    [cell.tipImageView setImage:[UIImage imageNamed:imageArr[indexPath.row - 1]]];
    cell.nameLabel.text = nameArr[indexPath.row - 1];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        if ([NSString checkNullWithNameString:[CXDHelper getUserAccountName]]) {
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            [self.navigationController pushViewController:loginVC animated:YES];
        }
    }else if (indexPath.row == 1) {
        MemerBroswerHistoryViewController *historyVC = [[MemerBroswerHistoryViewController alloc] init];
        [self.navigationController pushViewController:historyVC animated:YES];
    }else if (indexPath.row == 2) {
        MemberInfoEditViewController *infoVC = [[MemberInfoEditViewController alloc] init];
        [self.navigationController pushViewController:infoVC animated:YES];
    }else if (indexPath.row == 3) {
        MemberPwEditViewController *pwVC = [[MemberPwEditViewController alloc] init];
        [self.navigationController pushViewController:pwVC animated:YES];
    }else if (indexPath.row == 4) {
        [CXDHelper userLoginStatusWithBool:NO];
        [CXDHelper saveUserAccountWithAccountName:@""];
        MemberCenterTopTableViewCell *cell = [self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
        if ([NSString checkNullWithNameString:[CXDHelper getUserAccountName]]) {
            cell.nameLaebl.text = @"暂未登录";
        }else {
            if ([CXDHelper getUserLoginStatus]) {
                cell.nameLaebl.text = [NSString stringWithFormat:@"会员：%@",[CXDHelper getUserAccountName]];
            }else {
                cell.nameLaebl.text = @"暂未登录";
            }
        }
        [CXDNoticeView showNoticeViewWithString:@"当前用户已退出登录"];
        [CXDHelper saveUserAccountWithAccountName:nil];
        self.navigationItem.rightBarButtonItem = nil;
    }
    [self.myTableView reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 255;
    }
    return 50;
}

@end
